const Router=require('koa-router');
const homepage=new Router();
const sqldata = require('./sqldata');
homepage.get('/',async(ctx)=>{
    let sql;
    let mydata;
    let mydata1;
    let mydata3;
    let mydata4;
    let mydata5;
    let mydata6;
    let mydata7;
    let mydata2=[];
    let arr=[];
    console.log(ctx.request.query);
    if(ctx.request.query.btn==undefined){
        sql=`select * from user,works where user.Uid='${ctx.request.query.Bid}' and works.Uid='${ctx.request.query.Bid}'`
        mydata = await new Promise((resolve, reject)=>{
            return sqldata(sql, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
    }else if(ctx.request.query.btn=='作品'){
        sql=`select * from user,works where user.Uid='${ctx.request.query.Bid}' and works.Uid='${ctx.request.query.Bid}'`
        mydata = await new Promise((resolve, reject)=>{
            return sqldata(sql, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
    }else{
        sql=`select * from user,workfabulous,works where user.Uid=works.Uid and workfabulous.Uid='${ctx.request.query.Bid}' and works.Wid=workfabulous.Wid`
        mydata = await new Promise((resolve, reject)=>{
            return sqldata(sql, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
        for(let i=0;i<mydata.length;i++){
            mydata[i].img='/images/红色爱心.png'
        }
        sql=`select * from user where user.Uid='${ctx.request.query.Bid}'`
        mydata7= await new Promise((resolve, reject)=>{
            return sqldata(sql, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
    }
    sql=`select * from attention where Uid='${ctx.request.query.Uid}' and Bid='${ctx.request.query.Bid}'`
    mydata1 = await new Promise((resolve, reject)=>{
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    sql=`select count(*) as counts from attented where Uid='${ctx.request.query.Bid}'`
    mydata3= await new Promise((resolve, reject)=>{
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    sql=`select count(*) as counts from attention where Uid='${ctx.request.query.Bid}'`
    mydata4= await new Promise((resolve, reject)=>{
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    sql=`select sum(Wzan) as sums from works where Uid='${ctx.request.query.Bid}'`
    mydata5= await new Promise((resolve, reject)=>{
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    sql=`select sum(Czan) as sums from comment where Uid='${ctx.request.query.Bid}'`
    mydata6= await new Promise((resolve, reject)=>{
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    mydata2.push(mydata)
    if(mydata1==''){
        arr=[{Astate:'+关注',
            color:'white',
            bgcolor:'#ec5757'
            }];
        mydata2.push(arr)
     }else{
        mydata1[0].color='black';
        mydata1[0].bgcolor='lightgray'
        mydata2.push(mydata1)   
     }
    mydata2.push(mydata3)
    mydata2.push(mydata4)
    mydata2.push(mydata5)
    mydata2.push(mydata6)
    mydata2.push(mydata7)
    ctx.body=mydata2;
})
homepage.post('/',async(ctx)=>{
    console.log(ctx.request.body)
    let val=ctx.request.body;
    let mydata1;
    let mydata2;
    let mydata3;
    let sql;
    mydata1 = await new Promise((resolve, reject)=>{
        sql=`select * from attented where Uid='${val.Bid}' and Bid='${val.Uid}'`
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    mydata2 = await new Promise((resolve, reject)=>{
        sql=`select * from attented where Uid='${val.Uid}' and Bid='${val.Bid}'`
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    if(val.Astate=='已关注'){
        if(mydata1==''&& mydata2==''){
            mydata3 = await new Promise((resolve, reject)=>{
                sql=`insert into attented values ('${val.Bid}','${val.Uid}','${val.Time}','+关注')`
                return sqldata(sql, (err, data)=>{
                    if(err) reject(err);
                    resolve(data);	// 返回拿到的数据
                })
            })
        }else if(mydata1=='' && mydata2!==''){
            mydata3= await new Promise((resolve, reject)=>{
                sql=`insert into attented values ('${val.Bid}','${val.Uid}','${val.Time}','已关注')`
                return sqldata(sql, (err, data)=>{
                    if(err) reject(err);
                    resolve(data);	// 返回拿到的数据
                })
            })
            mydata3= await new Promise((resolve, reject)=>{
                sql=`update attented  set Time='${val.Time}',Astate='${val.Astate}' where Uid='${val.Uid}' and Bid='${val.Bid}'`
                return sqldata(sql, (err, data)=>{
                    if(err) reject(err);
                    resolve(data);	// 返回拿到的数据
                })
            })
        }
        mydata3= await new Promise((resolve, reject)=>{
            sql=`insert into attention values ('${val.Uid}','${val.Bid}','${val.Astate}')`
            return sqldata(sql, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
    }else{      
        if(mydata2==''){
            mydata3 = await new Promise((resolve, reject)=>{
                sql=`delete from attented where Uid='${val.Bid}' and Bid='${val.Uid}'`
                return sqldata(sql, (err, data)=>{
                    if(err) reject(err);
                    resolve(data);	// 返回拿到的数据
                })
            })
        }else if(mydata2!==''){
            mydata3 = await new Promise((resolve, reject)=>{
                sql=`delete from attented where Uid='${val.Bid}' and Bid='${val.Uid}'`
                return sqldata(sql, (err, data)=>{
                    if(err) reject(err);
                    resolve(data);	// 返回拿到的数据
                })
            })
            mydata3= await new Promise((resolve, reject)=>{
                sql=`update attented  set Time='${val.Time}',Astate='${val.Astate}' where Uid='${val.Uid}' and Bid='${val.Bid}'`
                return sqldata(sql, (err, data)=>{
                    if(err) reject(err);
                    resolve(data);	// 返回拿到的数据
                })
            })
        }
        mydata3 = await new Promise((resolve, reject)=>{
            sql=`delete from attention where Uid='${val.Uid}' and Bid='${val.Bid}'`
            return sqldata(sql, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
    }
})
//关注列表
homepage.get('/me-attenlist',async(ctx)=>{
    let mydata1;
    let mydata2;
    let sql;
    let val=ctx.request.query;
    mydata1 = await new Promise((resolve, reject)=>{
        sql=`select * from attention,user where attention.Uid='${val.Bid}' and user.Uid=attention.Bid`
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    for(let i=0;i<mydata1.length;i++){
        mydata2=await new Promise((resolve, reject)=>{
            sql=`select * from attention where Uid='${val.Uid}' and Bid='${mydata1[i].Uid}'`
            return sqldata(sql, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
        if(mydata2!=''){
            mydata1[i].Astate='已关注';
            mydata1[i].bgcolor='lightgray'
            mydata1[i].color='lightgray'
        }else{
            mydata1[i].Astate='+关注'
            mydata1[i].bgcolor='#ff986c'
            mydata1[i].color='#ff986c'
        }
    }
    ctx.body = mydata1;
})
homepage.post('/me-attenlist',async(ctx)=>{
    console.log(ctx.request.body)
    let val=ctx.request.body;
    let mydata1;
    let mydata2;
    let mydata3;
    let sql;
    mydata1 = await new Promise((resolve, reject)=>{
        sql=`select * from attented where Uid='${val.Bid}' and Bid='${val.Uid}'`
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    mydata2 = await new Promise((resolve, reject)=>{
        sql=`select * from attented where Uid='${val.Uid}' and Bid='${val.Bid}'`
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    if(val.Astate=='已关注'){
        if(mydata1==''&& mydata2==''){
            mydata3 = await new Promise((resolve, reject)=>{
                sql=`insert into attented values ('${val.Bid}','${val.Uid}','${val.Time}','+关注')`
                return sqldata(sql, (err, data)=>{
                    if(err) reject(err);
                    resolve(data);	// 返回拿到的数据
                })
            })
        }else if(mydata1=='' && mydata2!==''){
            mydata3= await new Promise((resolve, reject)=>{
                sql=`insert into attented values ('${val.Bid}','${val.Uid}','${val.Time}','已关注')`
                return sqldata(sql, (err, data)=>{
                    if(err) reject(err);
                    resolve(data);	// 返回拿到的数据
                })
            })
            mydata3= await new Promise((resolve, reject)=>{
                sql=`update attented  set Time='${val.Time}',Astate='${val.Astate}' where Uid='${val.Uid}' and Bid='${val.Bid}'`
                return sqldata(sql, (err, data)=>{
                    if(err) reject(err);
                    resolve(data);	// 返回拿到的数据
                })
            })
        }
        mydata3= await new Promise((resolve, reject)=>{
            sql=`insert into attention values ('${val.Uid}','${val.Bid}','${val.Astate}')`
            return sqldata(sql, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
    }else{      
        if(mydata2==''){
            mydata3 = await new Promise((resolve, reject)=>{
                sql=`delete from attented where Uid='${val.Bid}' and Bid='${val.Uid}'`
                return sqldata(sql, (err, data)=>{
                    if(err) reject(err);
                    resolve(data);	// 返回拿到的数据
                })
            })
        }else if(mydata2!==''){
            mydata3 = await new Promise((resolve, reject)=>{
                sql=`delete from attented where Uid='${val.Bid}' and Bid='${val.Uid}'`
                return sqldata(sql, (err, data)=>{
                    if(err) reject(err);
                    resolve(data);	// 返回拿到的数据
                })
            })
            mydata3= await new Promise((resolve, reject)=>{
                sql=`update attented  set Time='${val.Time}',Astate='${val.Astate}' where Uid='${val.Uid}' and Bid='${val.Bid}'`
                return sqldata(sql, (err, data)=>{
                    if(err) reject(err);
                    resolve(data);	// 返回拿到的数据
                })
            })
        }
        mydata3 = await new Promise((resolve, reject)=>{
            sql=`delete from attention where Uid='${val.Uid}' and Bid='${val.Bid}'`
            return sqldata(sql, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
    }
})
//粉丝列表
homepage.get('/me-fanlist',async(ctx)=>{    
    let mydata1;
    let mydata2;
    let sql;
    let val=ctx.request.query;
    mydata1 = await new Promise((resolve, reject)=>{
        sql=`select * from attented,user where attented.Uid='${val.Bid}' and user.Uid=attented.Bid`
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    for(let i=0;i<mydata1.length;i++){
        mydata2=await new Promise((resolve, reject)=>{
            sql=`select * from attention where Uid='${val.Uid}' and Bid='${mydata1[i].Uid}'`
            return sqldata(sql, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
        if(mydata2!=''){
            mydata1[i].Astate='已关注';
            mydata1[i].bgcolor='lightgray'
            mydata1[i].color='lightgray'
        }else{
            mydata1[i].Astate='+关注'
            mydata1[i].bgcolor='#ff986c'
            mydata1[i].color='#ff986c'
        }
    }
    ctx.body = mydata1;
})
homepage.post('/me-fanlist',async(ctx)=>{
    let val=ctx.request.body;
    console.log(val)
    let mydata1;
    let mydata2;
    let mydata3;
    let sql;
    mydata1 = await new Promise((resolve, reject)=>{
        sql=`select * from attented where Uid='${val.Bid}' and Bid='${val.Uid}'`
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    mydata2 = await new Promise((resolve, reject)=>{
        sql=`select * from attented where Uid='${val.Uid}' and Bid='${val.Bid}'`
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    if(val.Astate=='已关注'){
        if(mydata1==''&& mydata2==''){
            mydata3 = await new Promise((resolve, reject)=>{
                sql=`insert into attented values ('${val.Bid}','${val.Uid}','${val.Time}','+关注')`
                return sqldata(sql, (err, data)=>{
                    if(err) reject(err);
                    resolve(data);	// 返回拿到的数据
                })
            })
        }else if(mydata1=='' && mydata2!==''){
            mydata3= await new Promise((resolve, reject)=>{
                sql=`insert into attented values ('${val.Bid}','${val.Uid}','${val.Time}','已关注')`
                return sqldata(sql, (err, data)=>{
                    if(err) reject(err);
                    resolve(data);	// 返回拿到的数据
                })
            })
            mydata3= await new Promise((resolve, reject)=>{
                sql=`update attented  set Time='${val.Time}',Astate='${val.Astate}' where Uid='${val.Uid}' and Bid='${val.Bid}'`
                return sqldata(sql, (err, data)=>{
                    if(err) reject(err);
                    resolve(data);	// 返回拿到的数据
                })
            })
        }
        mydata3= await new Promise((resolve, reject)=>{
            sql=`insert into attention values ('${val.Uid}','${val.Bid}','${val.Astate}')`
            return sqldata(sql, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
    }else{      
        if(mydata2==''){
            mydata3 = await new Promise((resolve, reject)=>{
                sql=`delete from attented where Uid='${val.Bid}' and Bid='${val.Uid}'`
                return sqldata(sql, (err, data)=>{
                    if(err) reject(err);
                    resolve(data);	// 返回拿到的数据
                })
            })
        }else if(mydata2!==''){
            mydata3 = await new Promise((resolve, reject)=>{
                sql=`delete from attented where Uid='${val.Bid}' and Bid='${val.Uid}'`
                return sqldata(sql, (err, data)=>{
                    if(err) reject(err);
                    resolve(data);	// 返回拿到的数据
                })
            })
            mydata3= await new Promise((resolve, reject)=>{
                sql=`update attented  set Time='${val.Time}',Astate='${val.Astate}' where Uid='${val.Uid}' and Bid='${val.Bid}'`
                return sqldata(sql, (err, data)=>{
                    if(err) reject(err);
                    resolve(data);	// 返回拿到的数据
                })
            })
        }
        mydata3 = await new Promise((resolve, reject)=>{
            sql=`delete from attention where Uid='${val.Uid}' and Bid='${val.Bid}'`
            return sqldata(sql, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
    }
})
module.exports=homepage;